package com.bua.toothfactory;

import com.bua.datafactory.OrderListDataFactory;
import com.bua.net.NetConfig;
import com.bua.net.RequestTask;
import com.bua.net.ResponseReport;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.bua.net.Tool;

import java.util.HashMap;

public class OrderListActivity extends Activity implements ResponseReport {
    private OrderListDataFactory orderList = null;
    private ListView lvOrderList = null;

    private int CurPage = 1;
    private Button btnNextPage;
    private Button btnPrePage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);

        TextView orderTitle = (TextView) findViewById(R.id.orderTitle);
        String hospital = this.getIntent().getStringExtra("Hospital");
        String department = this.getIntent().getStringExtra("Department");
        orderTitle.setText(String.format("%s %s", hospital, department));
        Button btnCreateOrder = (Button) findViewById(R.id.btnCreateOrder);
        btnNextPage = new Button(this);
        btnPrePage = new Button(this);
        GetOrderList(CurPage);

        btnCreateOrder.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // startActivity()
                Intent intent = new Intent(OrderListActivity.this,
                        OrderDetailActivity.class);
                startActivity(intent);

            }
        });

        lvOrderList = (ListView) findViewById(R.id.lvOrderList);

        btnNextPage.setText("下一页");
        btnPrePage.setText("上一页");
        btnNextPage.setEnabled(false);
        btnPrePage.setEnabled(false);

        btnNextPage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO 自动生成的方法存根
                CurPage++;
                btnNextPage.setEnabled(false);
                btnPrePage.setEnabled(false);
                GetOrderList(CurPage);
            }
        });

        btnPrePage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO 自动生成的方法存根
                CurPage--;
                btnNextPage.setEnabled(false);
                btnPrePage.setEnabled(false);
                GetOrderList(CurPage);
            }
        });

        lvOrderList.addFooterView(btnPrePage);
        lvOrderList.addFooterView(btnNextPage);

        lvOrderList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // TODO Auto-generated method stub

                String orderId = orderList.orderItem[arg2].orderId;
                Intent intent = new Intent(OrderListActivity.this,
                        OrderDetailActivity.class);
                intent.putExtra("orderId", orderId);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onRestart()
    {
         super.onRestart();
         GetOrderList(CurPage);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_order_list, menu);
        return true;
    }

    private void GetOrderList(int page) {
        String url = NetConfig.creatUrl(NetConfig.HttpRequestId.GET_ORDER_LIST);
        HashMap<String, String> requestData = NetConfig.GetOrderList(page);
        RequestTask request = new RequestTask();
        request.CreateTask(url, requestData, NetConfig.HttpRequestId.GET_ORDER_LIST.ordinal(), this);

        //HttpConnection httpConnection=new HttpConnection(NetConfig.HttpRequestId.GET_ORDER_LIST.ordinal());

    }

    public class OrderListAdapter extends BaseAdapter {
        public Context mContext;
        public OrderListDataFactory OrderList;

        public OrderListAdapter(Context context, OrderListDataFactory orderList) {
            super();
            mContext = context;
            OrderList = orderList;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (this.OrderList == null || this.OrderList.orderItem == null)
                return 0;
            else
                return this.OrderList.orderItem.length;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            LinearLayout itemLayout = new LinearLayout(mContext);
            itemLayout.setPadding(0, 15, 0, 0);
            // 锟斤拷锟斤拷锟斤拷锟�
            TextView tvOrderID = new TextView(mContext);
            tvOrderID.setWidth(0);
            tvOrderID
                    .setLayoutParams(new LinearLayout.LayoutParams(
                            android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
            tvOrderID.setText(this.OrderList.orderItem[arg0].orderId);
            tvOrderID.setTextSize(20.f);
            itemLayout.addView(tvOrderID);

            // 锟斤拷锟斤拷锟斤拷锟斤拷
            TextView tvPatientName = new TextView(mContext);
            tvPatientName.setWidth(0);
            tvPatientName
                    .setLayoutParams(new LinearLayout.LayoutParams(
                            android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
            tvPatientName.setText(this.OrderList.orderItem[arg0].patientName);
            tvPatientName.setTextSize(20.f);
            itemLayout.addView(tvPatientName);

            TextView tvDentistName = new TextView(mContext);
            tvDentistName.setWidth(0);
            tvDentistName
                    .setLayoutParams(new LinearLayout.LayoutParams(
                            android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
            tvDentistName.setText(this.OrderList.orderItem[arg0].dentistName);
            tvDentistName.setTextSize(20.f);
            itemLayout.addView(tvDentistName);

            TextView tvCreatedDate = new TextView(mContext);
            tvCreatedDate.setWidth(0);
            tvCreatedDate
                    .setLayoutParams(new LinearLayout.LayoutParams(
                            android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
            tvCreatedDate.setText(this.OrderList.orderItem[arg0].createdDate);
            tvCreatedDate.setTextSize(20.f);
            itemLayout.addView(tvCreatedDate);

            TextView tvUpdateDate = new TextView(mContext);
            tvUpdateDate.setWidth(0);
            tvUpdateDate
                    .setLayoutParams(new LinearLayout.LayoutParams(
                            android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
            tvUpdateDate.setText(this.OrderList.orderItem[arg0].updateDate);
            tvUpdateDate.setTextSize(20.f);
            itemLayout.addView(tvUpdateDate);

            return itemLayout;
        }

    }

    @Override
    public void callBack(String result, int requestId) {
        // TODO 自动生成的方法存根
        orderList = new OrderListDataFactory(result);
        if (orderList.status == 200) {
            lvOrderList.setAdapter(new OrderListAdapter(this, orderList));
            if (orderList.page == orderList.total) {
                btnNextPage.setEnabled(false);
                btnPrePage.setEnabled(true);
            }
            if (orderList.page == 1) {
                btnNextPage.setEnabled(true);
                btnPrePage.setEnabled(false);
            }
            if (orderList.total == 1) {
                btnNextPage.setEnabled(false);
                btnPrePage.setEnabled(false);
            }
        } else {
            Tool.showToast(this, orderList.message);
        }
    }


}
